package com.proactive.webapp.controller;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
















import com.proactive.webapp.dao.CardDAO;
import com.proactive.webapp.dao.LightDAO;
import com.proactive.webapp.dao.LightTemplateDAO;
import com.proactive.webapp.dao.MemberDAO;
import com.proactive.webapp.dao.MenuDAO;
import com.proactive.webapp.entity.Card;
import com.proactive.webapp.entity.Light;
import com.proactive.webapp.entity.LightTemplate;
import com.proactive.webapp.entity.Member;
import com.proactive.webapp.entity.Menu;
import com.proactive.webapp.util.HtmlUtil;
import com.proactive.webapp.util.MenuUtil;



@Controller
public class LightController {
	ApplicationContext context =  new ClassPathXmlApplicationContext("spring-module.xml");
	@RequestMapping(value = "/lantern", method = RequestMethod.GET)
	public ModelAndView getLantern(String id){
		ModelAndView model = new ModelAndView("light");
		LightDAO lightDAO = (LightDAO) context.getBean("lightDAO");
		LightTemplateDAO lightTemplateDAO = (LightTemplateDAO) context.getBean("lightTemplateDAO");
		List<Light> lightTenList = new ArrayList<Light>();
		List<LightTemplate> lightTemplateList = new ArrayList<LightTemplate>();
		
		List<Menu> menuList = MenuUtil.getList(((MenuDAO)context.getBean("menuDAO")).getList(), 5);
		
		if(id==null){
			lightTenList=lightDAO.getNextTenList(0,0);
		}else{
			lightTenList=lightDAO.getNextTenList(0,Integer.parseInt(id));
		}
		lightTemplateList=lightTemplateDAO.getList();
		model.addObject("lightTenList", lightTenList);
		model.addObject("total", lightDAO.getTotal());
		model.addObject("lightTemplateList", lightTemplateList);
		model.addObject("menuList", menuList);
		return model;
	}
	
	@RequestMapping(value = "/showNextLight", method = RequestMethod.POST)
	public @ResponseBody List<Light> showNextLight(int now,int id){
		LightDAO lightDAO = (LightDAO) context.getBean("lightDAO");
		List<Light> lightTenList = new ArrayList<Light>();
		lightTenList=lightDAO.getNextTenList(now,id);
		return lightTenList;
	}
	
	@RequestMapping(value = "/addLight", method = RequestMethod.POST)
	public @ResponseBody int ajax(Light light,HttpSession session){
		Member loginMember = (Member)session.getAttribute("loginMember");
		light.setAccount(loginMember.getAccount());
		LightDAO lightDAO = (LightDAO)context.getBean("lightDAO");
		int id=lightDAO.insert(light);
		return id;
	}
	
	@RequestMapping(value = "/memberLight", method = RequestMethod.GET)
	public ModelAndView getMemberLightList(HttpServletRequest request, HttpSession session) {
		ModelAndView model = new ModelAndView();
		Member loginMember = (Member)session.getAttribute("loginMember");
		if(loginMember != null){
		MemberDAO memberDAO = (MemberDAO) context.getBean("memberDAO");
		LightDAO lightDAO = (LightDAO)context.getBean("lightDAO");
		Member member = new Member();			
		List<Light> lightList = new ArrayList<Light>();
		
		String memberNO = request.getParameter("id");
		member.setMemberNO(memberNO);
		member = memberDAO.getByMemberNO(member);//用memberNO取得memberAccount
		if(member.getAccount()!=null){
			if(loginMember != null)
				member = memberDAO.getByLogin(member, loginMember.getAccount());//取得member資料
			
		List<Menu> menuList = MenuUtil.getList(((MenuDAO)context.getBean("menuDAO")).getList(), 0);
			
			lightList=lightDAO.getListByMember(member.getAccount());
			model.addObject("member", member);
			model.addObject("lightList", lightList);
			model.addObject("menuList", menuList);
			model.setViewName("memberLight");
		}
		else
			model.setViewName("redirect:/lantern");
	} 
	else
		model.setViewName(loginMember!=null?"redirect:/memberLight?id="+loginMember.getMemberNO():"redirect:/lantern");
		
		return model;
   }
	
	@RequestMapping(value = "/lightHtml", method = RequestMethod.GET)
	public ModelAndView getLight(HttpServletRequest request, HttpSession session) {
		LightDAO lightDAO = (LightDAO) context.getBean("lightDAO");
		LightTemplateDAO lightTemplateDAO = (LightTemplateDAO) context.getBean("lightTemplateDAO");
		ModelAndView model = new ModelAndView("lightHtml");
		if(request.getParameter("id")!=null && !request.getParameter("id").equals("")){
			int lightID = Integer.parseInt(request.getParameter("id"));
			
			Light light = new Light();
			light.setId(lightID);
			light=lightDAO.get(light);
			
			light.setReceiver(light.getReceiver().replaceAll("(\r\n|\n)", "<br>").replaceAll(" ", "&nbsp;"));
			light.setContent(light.getContent().replaceAll("(\r\n|\n)", "<br>").replaceAll(" ", "&nbsp;"));
			light.setSender(light.getSender().replaceAll("(\r\n|\n)", "<br>").replaceAll(" ", "&nbsp;"));
			
			
			LightTemplate lightTemplate = new LightTemplate();
			lightTemplate.setId(light.getTid());
			lightTemplate = lightTemplateDAO.get(lightTemplate);
			
			HtmlUtil htmlUtil = new HtmlUtil();
			String message = "";
			try {
				message = htmlUtil.htmlToString(request.getServletContext().getRealPath(lightTemplate.getHtml1Path()));
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			message = message.replace("example_receiver", light.getReceiver());
			message = message.replace("example_text", light.getContent());
			message = message.replace("example_sender", light.getSender());
			model.addObject("message", message);
		}
		return model;
	}
	
	@RequestMapping(value = "/lightHtml2", method = RequestMethod.GET)
	public ModelAndView getLight2(HttpServletRequest request, HttpSession session) {
		LightDAO lightDAO = (LightDAO) context.getBean("lightDAO");
		LightTemplateDAO lightTemplateDAO = (LightTemplateDAO) context.getBean("lightTemplateDAO");
		ModelAndView model = new ModelAndView("lightHtml2");
		
		Member loginMember = (Member)session.getAttribute("loginMember");
		MemberDAO memberDAO = (MemberDAO) context.getBean("memberDAO");
		Member member = new Member();	
		
		if(request.getParameter("id")!=null && !request.getParameter("id").equals("")){
			
			
			int lightID = Integer.parseInt(request.getParameter("id"));
			Light light = new Light();
			light.setId(lightID);
			light=lightDAO.get(light);
			
			light.setQuestion(light.getQuestion().replaceAll("(\r\n|\n)", "<br>").replaceAll(" ", "&nbsp;"));
			
			
			LightTemplate lightTemplate = new LightTemplate();
			lightTemplate.setId(light.getTid());
			lightTemplate = lightTemplateDAO.get(lightTemplate);
			
			HtmlUtil htmlUtil = new HtmlUtil();
			String message = "";
			try {
				message = htmlUtil.htmlToString(request.getServletContext().getRealPath(lightTemplate.getHtml2Path()));
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			message = message.replace("example_question", light.getQuestion());
			
			String memberNO = request.getParameter("id");
			member.setMemberNO(memberNO);
			member = memberDAO.getByMemberNO(member);//用memberNO取得memberAccount
		
			if(loginMember != null){
				message = message.replace("example_answer", light.getAnswer());
			}
						
			model.addObject("message", message);
		}
		return model;
	}
	
	@RequestMapping(value = "/checkLightStatus", method = RequestMethod.POST)
	public @ResponseBody String checkLightStatus(Light light,HttpSession session,int id){
		
		LightDAO lightDAO = (LightDAO) context.getBean("lightDAO");
		light.setId(id);
		light=lightDAO.get(light);
		
		String html="";
		
		if(light.getStatus()==0){
			html="http://localhost:8080/Proactive/lightHtml?id="+id;
		}
		else if(light.getStatus()==1){
			html="http://localhost:8080/Proactive/lightHtml2?id="+id;
		}
		return html;
	}
	
	@RequestMapping(value = "/checkAnswer", method = RequestMethod.POST)
	public @ResponseBody boolean checkAnswer(Light light,HttpServletRequest request,String id){
		
		LightDAO lightDAO = (LightDAO)context.getBean("lightDAO");
		light.setId(Integer.parseInt(id));
		light=lightDAO.get(light);
		
		boolean flag=false;
		String answer=request.getParameter("riddleAns");
		
		if(light.getAnswer().equals(answer)){
			flag=true;
		}
		
		return flag;
		
	}
	
	@RequestMapping(value = "/checkWho", method = RequestMethod.POST)
	public @ResponseBody boolean checkWho(HttpSession session,String id){
		Member loginMember = (Member)session.getAttribute("loginMember");
		LightDAO lightDAO = (LightDAO)context.getBean("lightDAO");
		boolean flag=false;
		
		Light light = new Light();
		light.setId(Integer.parseInt(id));
		light=lightDAO.get(light);
		
		String memberAccount=loginMember.getAccount();
		
		if(light.getAccount().equals(memberAccount))
			flag=true;
		
		return flag;
	}
	
	@RequestMapping(value = "/delLight", method = RequestMethod.POST)
	public ModelAndView actCard(int id, HttpSession session, HttpServletRequest request) {
		Member loginMember = (Member)session.getAttribute("loginMember");
		ModelAndView model = new ModelAndView();
		if(loginMember != null){//檢查登入
			LightDAO lightDAO = (LightDAO)context.getBean("lightDAO");
			Light light = new Light();
			light.setId(id);
			light=lightDAO.get(light);
			if(light.getAccount().equals(loginMember.getAccount())){//檢查是否為作者
					lightDAO.delete(light);
				
				model.setViewName("redirect:"+request.getHeader("referer"));
			}
			else 
				model.setViewName("redirect:/memberLight");
		}
		else
			model.setViewName("redirect:/login");
		return model;
 
	}
	
}
